package com.my.internetplus.controller;

import com.my.internetplus.model.Cart;
import com.my.internetplus.result.BaseResult;
import com.my.internetplus.service.CartService;
import com.my.internetplus.service.UserService;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
public class CartController {

    @Resource
    private UserService userService;

    @Resource
    private CartService cartService;

    @RequestMapping("insertCart")
    public void insertCart(String openid,int goodid,int sum){

        cartService.insertCart(userService.selectUserByOpenId(openid).getUserId(),goodid,sum);

    }

    @RequestMapping("selectCartList")
    public BaseResult<List<Map<String,String>>> selectCartList(String openid){
        System.out.println(cartService.selectCartInfoList(userService.selectUserByOpenId(openid).getUserId()));

        return BaseResult.success(cartService.selectCartInfoList(userService.selectUserByOpenId(openid).getUserId()));
    }

    @RequestMapping("updateCart")
    public BaseResult<List<Map<String,String>>> updateCart(int cartid,int sum,String openid){
        cartService.updateCart(cartid,sum);

        return BaseResult.success(cartService.selectCartInfoList(userService.selectUserByOpenId(openid).getUserId()));

    }

    @RequestMapping("subCart")
    public BaseResult<List<Map<String,String>>> subCart(int cartid,String openid){
        cartService.subCart(cartid);

        return BaseResult.success(cartService.selectCartInfoList(userService.selectUserByOpenId(openid).getUserId()));

    }
    @RequestMapping("addCart")
    public BaseResult<List<Map<String,String>>> addCart(int cartid,String openid){
        cartService.addCart(cartid);

        return BaseResult.success(cartService.selectCartInfoList(userService.selectUserByOpenId(openid).getUserId()));

    }

    @RequestMapping("selectCartListsById")
    public BaseResult<List<Map<String,String>>> selectCartListsById(String idlist){
        String replace = idlist.replace("\"", "");
        String[] split = replace.split(",");
        List list = Arrays.asList(split);
        return BaseResult.success(cartService.selectCartListsById(list));

    }


}
